package com.lwj.diao;

import com.lwj.diao.Utils.UtilOut;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.IOException;
import java.util.Map;

public class GetResult implements HttpHandler {
    final byte[] mIndexBytes = Htmls.HTML_INDEX.toString().getBytes(Htmls.UTF8_CS);

    @Override
    public void handle(HttpExchange exchange) throws IOException {
        try {
            String resultHtml = null;
            Map<String, String> params = Htmls. convertStream2Params(exchange);
            String code_id = params.get("codeId");
            String val = params.get("subVal");
            if (code_id != null) {
                resultHtml = WebThreads.subValue(code_id, val);
            } else {
                String source = this.codeHandle(params.get("java_source"));
                resultHtml = WebThreads.parseExecuteResult(source);
            }
            byte[] finalHtmlBytes = resultHtml.getBytes(Htmls.UTF8_CS);
            final Headers responseHeaders = exchange.getResponseHeaders();
            responseHeaders.set("Access-Control-Allow-Origin", "*");
            responseHeaders.set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
            responseHeaders.set("Access-Control-Allow-Headers", "x-requested-with");
            responseHeaders.set("Access-Control-Max-Age", "1800");
            exchange.sendResponseHeaders(200, finalHtmlBytes.length);
            exchange.getResponseBody().write(finalHtmlBytes);
            exchange.getResponseBody().close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 代码格式补充
     * @return
     */
    private String codeHandle(String source) {
        source = mainHandle(source);
        source = classHandle(source);
        source = packageHandle(source);
        return source;
    }

    private String mainHandle(String source) {
        if (source.indexOf(" main") > -1) {
            return source;
        }
        return "public static void main(String[] args) {\n" +
                source +
                "        \n" +
                "    }";
    }

    private String classHandle(String source) {
        if (source.indexOf("class ") > -1) {
            return source;
        }
        String className = "L" + System.currentTimeMillis();
        return "public class "+className+ " {\n" +
                source +
                "}";
//
//        if (source.indexOf("class ") < 0) {
//            if (source.indexOf("import") > -1) {
//                int anImport = source.indexOf(";", source.lastIndexOf("import"));
//                return this.packageHandle(source.substring(0, anImport) + "public class " + className + " {" + source.substring(anImport) + "}");
//            }
//            return this.packageHandle("public class " + className + " {" + source + "}");
//        }
//        return this.packageHandle(source);
    }

    private String packageHandle(String source) {
        if (source.indexOf("package") > -1) {
            return source.substring(0, source.indexOf(";") + 1) + "import java.util.Scanner;" + source.substring(source.indexOf(";") + 1);
        }
        return "package com.lwj.diao;import java.util.Scanner;" + source;
    }
}
